//
//  UINavigationBar-back.swift
//  ETNavBarTransparentDemo
//
//  Created by zhu xietong on 2017/9/29.
//  Copyright © 2017年 zhuxieong. All rights reserved.
//

import Foundation
import UIKit



extension UINavigationController
{
    func  indexOf(controller:UIViewController)->Int?{
        let ctrs = self.viewControllers
        var index:Int? = nil
        for (i,one) in ctrs.enumerated()
        {
            if one === controller
            {
                index = i
                break
            }
        }
        return index
        
    }
}
var indexooo = 0

extension UIViewController
{
    var navPreController:UIViewController?{
        get{
            var ctr:UIViewController? = nil
            guard let ctrs = self.navigationController?.viewControllers else {
                return nil
            }
            var index:Int? = nil
            for (i,one) in ctrs.enumerated()
            {
                if one === self
                {
                    index = i
                    break
                }
            }
            if let i = index
            {
                if i > 0
                {
                    ctr = ctrs[i-1]
                }
            }
            return ctr
        }
    }
}

extension UINavigationBar{
    

    
    func updateLeftStyle(with controller:UIViewController) {
//        self.leftBack.style = controller.top_style
    }
    
    func updateRightStyle(with controller:UIViewController) {
//        self.rightBack.style = controller.top_style
    }
    
    
    
    func stop(at controller:UIViewController){        

        tintColor = controller.top_style.navTintColor
        titleTextAttributes = controller.top_style.titleTextAttributes
//        self.centerBack.style = controller.top_style
        
//        self.x_constrain?.constant = 0
        self.layoutIfNeeded()
//        mo_back.layoutIfNeeded()
        
//        self.leftBack.style = controller.navPreController?.top_style ?? .default

       
    }
    

    
    func popTo(controller:UIViewController?,animated:Bool) {
        if let acontroller = controller{
//            self.leftBack.style = acontroller.top_style
            if animated
            {
//                UIView.beginAnimations(nil, context: nil)
//                UIView.setAnimationDuration(EeTopNavigationDelegate.duration)
//                UIView.setAnimationCurve(EeTopNavigationDelegate.animationCurve)
//                self.x_constrain?.constant = self.frame.size.width
                titleTextAttributes = acontroller.top_style.titleTextAttributes

//                mo_back.layoutIfNeeded()
//                UIView.commitAnimations()
//                weak var wself = self
//                co_delay(delay: EeTopNavigationDelegate.duration, closure: {
//                    wself?.stop(at: acontroller)
//                })
            }
            else{
                weak var wself = self

//                co_delay(delay: 0, closure: {
//
//                    self.x_constrain?.constant = self.frame.size.width
//                    self.mo_back.layoutIfNeeded()
//                })
                    wself?.stop(at: acontroller)
            }
        }
    }
    
    func updatePopScale(scale:CGFloat) {
//        self.x_constrain?.constant = (self.frame.size.width) * scale
//        mo_back.layoutIfNeeded()
    }
    
    func pushTo(controller:UIViewController,animated:Bool) {

//        self.leftBack.style = self.navigationController?.topViewController?.top_style ?? .default
//        self.centerBack.style = self.navigationController?.topViewController?.top_style ?? .default
//        self.rightBack.style = controller.top_style
//        self.x_constrain?.constant = -self.frame.size.width
//        mo_back.layoutIfNeeded()
//        weak var wself = self
//        co_delay(delay: EeTopNavigationDelegate.duration, closure: {
//            //                wself?.leftBack.style = leftCtr?.top_style ?? .default
//            wself?.stop(at: controller)
//        })

//        if animated
//        {
////            UIView.beginAnimations(nil, context: nil)
////            UIView.setAnimationDuration(EeTopNavigationDelegate.duration)
////            UIView.setAnimationCurve(EeTopNavigationDelegate.animationCurve)
//            self.x_constrain?.constant = -self.frame.size.width
//            mo_back.layoutIfNeeded()
////            UIView.commitAnimations()
//            weak var wself = self
//            co_delay(delay: EeTopNavigationDelegate.duration, closure: {
////                wself?.leftBack.style = leftCtr?.top_style ?? .default
//                wself?.stop(at: controller)
//            })
//        }
//        else{
//            self.x_constrain?.constant = -self.frame.size.width
////            leftBack.style = leftCtr?.top_style ?? .default
//            stop(at: controller)
//        }
    }
}


public class MoShadowView: UIImageView {
    public static var color = UIColor(white: 0.3, alpha: 0.5)
    public var style = EeShadowStyle.none{
        didSet{
            switch style {
            case .color(let color):
                self.alpha = 1
                self.backgroundColor = color
            default:
                self.alpha = 0
            }
        }
    }
}

open class MoBackView: UIView {
    
    public let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .extraLight))
    public let imageView = UIImageView()
    public let shadowImageV = MoShadowView()
    public init() {
        super.init(frame: .zero)
        shadowImageV.tag = 110
        let vs:[UIView] = [blurView,imageView,shadowImageV]
        for (i,one) in vs.enumerated(){
            addSubview(one)
            one.translatesAutoresizingMaskIntoConstraints = false
            let l = NSLayoutConstraint(item: one, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0)
            let b = NSLayoutConstraint(item: one, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
            let r = NSLayoutConstraint(item: one, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0)
            if i == 2
            {
                let h = NSLayoutConstraint(item: one, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 0.6)
                addConstraints([h,l,b,r])
            }
            else{
                let t = NSLayoutConstraint(item: one, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
                addConstraints([t,l,b,r])
            }
        }
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open var style:EeTopStyle = .default
        {
        didSet{

            self.buildStyle()
        }
    }
    
    
    public var blurStyle = UIBlurEffect.Style.light
    {
        didSet{
            blurView.effect = UIBlurEffect(style: blurStyle)
        }
    }
    
  
    public func buildStyle() {
        
        self.shadowImageV.style = style.navBarShadow

        self.alpha = style.navBarBgAlpha
        switch style.navBarBgStyle {
        case .color(let color):
            blurView.alpha = 0
            imageView.alpha = 0
            self.backgroundColor = color
        case .blur(let blur):
            self.backgroundColor = .clear
            imageView.alpha = 0
            blurView.alpha = 1
            blurView.effect = UIBlurEffect(style: blur)
        case .image(let img):
            self.backgroundColor = .clear
            blurView.alpha = 0
            imageView.alpha = 1
            imageView.image = img
            break

        default:
            blurView.alpha = 0
            imageView.alpha = 0
            self.backgroundColor = .clear
        }
    }
   
}




//extension UINavigationBar{
//    fileprivate struct AssociatedBackKeys {
//        static var pushScale: CGFloat = 1.0
//    }
//    
//    open var pushScale: CGFloat {
//        get {
//            guard let _scale = objc_getAssociatedObject(self, &AssociatedBackKeys.pushScale) as? CGFloat else {
//                return 1.0
//            }
//            return _scale
//        }
//        set {
//            let _scale = max(min(newValue, 1), 0) // 必须在 0~1的范围
//            objc_setAssociatedObject(self, &AssociatedBackKeys.pushScale, _scale, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
//        }
//    }
//}


//extension UINavigationBar
//{
//
//    static var centerTag:Int = 29990
//    static var leftTag:Int = 299991
//    static var rightTag:Int = 299992
//
//
//
//    public var centerBack:MoBackView{
//        get{
//            if let left =  mo_back.viewWithTag(UINavigationBar.centerTag) as? MoBackView
//            {
//                return left
//            }
//            let one = MoBackView()
//            one.tag = UINavigationBar.centerTag
//            let back = mo_back
//            back.addSubview(one)
//            one.translatesAutoresizingMaskIntoConstraints = false
//
//            let t = NSLayoutConstraint(item: one, attribute: .top, relatedBy: .equal, toItem: back, attribute: .top, multiplier: 1, constant: 0)
//            let l = NSLayoutConstraint(item: one, attribute: .centerX, relatedBy: .equal, toItem: back, attribute: .centerX, multiplier: 1, constant: 0)
//            let b = NSLayoutConstraint(item: one, attribute: .bottom, relatedBy: .equal, toItem: back, attribute: .bottom, multiplier: 1, constant: 0)
//            let r = NSLayoutConstraint(item: one, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: Swidth)
//            back.addConstraints([t,l,b,r])
//            l.priority = .required
//
//            return one
//        }
//    }
//
//    public var rightBack:MoBackView{
//        get{
//            if let right =  mo_back.viewWithTag(UINavigationBar.rightTag) as? MoBackView
//            {
//                return right
//            }
//            let one = MoBackView()
//            one.tag = UINavigationBar.rightTag
//            let back = mo_back
//            back.addSubview(one)
//            one.translatesAutoresizingMaskIntoConstraints = false
//            let t = NSLayoutConstraint(item: one, attribute: .top, relatedBy: .equal, toItem: back, attribute: .top, multiplier: 1, constant: 0)
//            let l = NSLayoutConstraint(item: one, attribute: .left, relatedBy: .equal, toItem: centerBack, attribute: .right, multiplier: 1, constant: 0)
//            let b = NSLayoutConstraint(item: one, attribute: .bottom, relatedBy: .equal, toItem: back, attribute: .bottom, multiplier: 1, constant: 0)
//            let r = NSLayoutConstraint(item: one, attribute: .width, relatedBy: .equal, toItem: back, attribute: .width, multiplier: 1, constant: 0)
//            back.addConstraints([t,l,b,r])
//            l.priority = .required
//            return one
//        }
//    }
//    public var leftBack:MoBackView{
//        get{
//            if let left =  mo_back.viewWithTag(UINavigationBar.leftTag) as? MoBackView
//            {
//                return left
//            }
//            let one = MoBackView()
//            one.tag = UINavigationBar.leftTag
//            let back = mo_back
//            back.addSubview(one)
//            one.translatesAutoresizingMaskIntoConstraints = false
//
//            let t = NSLayoutConstraint(item: one, attribute: .top, relatedBy: .equal, toItem: back, attribute: .top, multiplier: 1, constant: 0)
//            let l = NSLayoutConstraint(item: one, attribute: .right, relatedBy: .equal, toItem: centerBack, attribute: .left, multiplier: 1, constant: 0)
//            let b = NSLayoutConstraint(item: one, attribute: .bottom, relatedBy: .equal, toItem: back, attribute: .bottom, multiplier: 1, constant: 0)
//            let r = NSLayoutConstraint(item: one, attribute: .width, relatedBy: .equal, toItem: back, attribute: .width, multiplier: 1, constant: 0)
//            back.addConstraints([t,l,b,r])
//            l.priority = .required
//            return one
//        }
//    }
//
//    func buildMoBack() {
////        if let _ = backgoundView.viewWithTag(89900)
////        {return}
//        _ = mo_back
//        _ = centerBack
//        _ = rightBack
//        _ = leftBack
//        self.shadowImage = UIImage.newImage(.clear)
//        self.setBackgroundImage(UIImage.newImage(.clear), for: .default)
//    }
//
//
//    public var mo_back:UIView{
//        let back = backgoundView
//        if let mo_bk = back.viewWithTag(89900)
//        {
//            return mo_bk
//        }
//        let mo_bk = UIView()
//        mo_bk.tag = 89900
//        mo_bk.translatesAutoresizingMaskIntoConstraints = false
//        back.addSubview(mo_bk)
//        let t = NSLayoutConstraint(item: mo_bk, attribute: .top, relatedBy: .equal, toItem: back, attribute: .top, multiplier: 1, constant: 0)
//        let l = NSLayoutConstraint(item: mo_bk, attribute: .centerX, relatedBy: .equal, toItem: back, attribute: .centerX, multiplier: 1, constant: 0)
//        let b = NSLayoutConstraint(item: mo_bk, attribute: .width, relatedBy: .equal, toItem: back, attribute: .width, multiplier: 3, constant: 0)
//        let r = NSLayoutConstraint(item: mo_bk, attribute: .bottom, relatedBy: .equal, toItem: back, attribute: .bottom, multiplier: 1, constant: 0)
//        x_constrain = l
//        back.addConstraints([t,l,b,r])
//        return mo_bk
//    }
//
//
//    public var backgoundView:UIView{
//        get{
//            let bk = self.subviews[0]
//            return bk
//        }
//    }
//}

//extension UINavigationBar{
//
//    fileprivate struct back_x_constrain {
//        static var XConstrain: String = "back_x_constrain"
//        static var navigationController: String = "navigationController"
//
//    }
//
//    public weak var navigationController:UINavigationController?{
//        get {
//            guard let ctr = objc_getAssociatedObject(self, &back_x_constrain.navigationController) as? UINavigationController else {
//                return nil
//            }
//            return ctr
//        }
//
//        set {
//            objc_setAssociatedObject(self, &back_x_constrain.navigationController, newValue, .OBJC_ASSOCIATION_ASSIGN)
//        }
//    }
//
//    public var x_constrain:NSLayoutConstraint?{
//        get {
//            guard let con = objc_getAssociatedObject(self, &back_x_constrain.XConstrain) as? NSLayoutConstraint else {
//                return nil
//            }
//            return con
//        }
//
//        set {
//            objc_setAssociatedObject(self, &back_x_constrain.XConstrain, newValue, .OBJC_ASSOCIATION_ASSIGN)
//        }
//    }
//}

extension UIImage{
    static func newImage(_ color:UIColor,size:CGSize=CGSize(width: 1, height: 1))  -> UIImage{
        let rect:CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}




